package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import com.example.easeimpro.R;
import com.example.easeimpro.controller.adapter.NewFriendListAdapter;
import com.example.easeimpro.model.bean.InviteInfo;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.db.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

public class NewFriendActivity extends BaseActivity {
    private RecyclerView mFriendList;
    private EaseTitleBar titleBar;
    private NewFriendListAdapter adapter;
    private LocalBroadcastManager mLBM;
    private BroadcastReceiver ContactChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //接受联系人广播
            //只要接受到联系人变化 就刷新
            refresh();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);
       initView();

        initData();
    }

    private void initData() {
        adapter = new NewFriendListAdapter(mContext, new NewFriendListAdapter.OnInviteListener() {
            @Override
            public void onAccept(InviteInfo info,int pos) {
                try {

                    EMClient.getInstance().contactManager().acceptInvitation(info.getUser().getHxId()); //接受邀请
                    //数据库更新
                    Model.getInstance().getDbManager().getInviteTableDao().updateInviteStatus(InviteInfo.InvationStatus.INVITE_ACCEPT,info.getUser().getHxId());
                    //页面变化
                     showToast("接受添加请求");

                     refresh();
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    showToast("接受添加请求失败");
                }
            }

            @Override
            public void onReject(InviteInfo info,int pos) {
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //通过本地用户id 通知环信服务器拒绝邀请
                            EMClient.getInstance().contactManager().deleteContact(info.getUser().getHxId());
                            //数据库变化
                            Model.getInstance().getDbManager().getInviteTableDao().removeInviteStatus(info.getUser().getHxId());

                            //页面变化
                            showToast("拒绝添加请求");
                            deleteRefresh(pos);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                            showToast("拒绝添加请求");
                        }

                    }
                });

            }
        });
        mFriendList.setAdapter(adapter);
        mFriendList.setLayoutManager(new LinearLayoutManager(mContext));
        refresh();

    }

    private void refresh() {
        //获取数据库所有邀请信息
        List<InviteInfo> invites = Model.getInstance().getDbManager().getInviteTableDao().getInvites();
        adapter.refresh(invites);//刷新适配器
        mLBM = LocalBroadcastManager.getInstance(mContext);//注册联系人信息变化的广播
        mLBM.registerReceiver(ContactChangedReceiver,new IntentFilter(Constant.CONTACT_INVITE_CHANGED));
    }

    private void deleteRefresh(int pos){
        List<InviteInfo> invites = Model.getInstance().getDbManager().getInviteTableDao().getInvites();
        adapter.deleteRefresh(invites,pos);//刷新适配器
        mLBM = LocalBroadcastManager.getInstance(mContext);//注册联系人信息变化的广播
        mLBM.registerReceiver(ContactChangedReceiver,new IntentFilter(Constant.CONTACT_INVITE_CHANGED));
    }

    private void initView() {
        titleBar = findViewById(R.id.new_friends_titleBar);
        mFriendList = findViewById(R.id.new_friends_list);
        titleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                finish();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLBM.unregisterReceiver(ContactChangedReceiver);
    }
}